﻿Imports SPRemoteService.Impl

Public Class SharePointTreeNode
    Inherits Windows.Forms.TreeNode

    Private _spContainer As SharePointContainer
    Private _queryState As QueryStateEnum = QueryStateEnum.Unknown
    Private _siteUrl As Uri
    Private _containerFolderPath As String = Nothing

    Public Sub New(ByVal container As SharePointContainer, ByVal itemLimit As Integer)
        Text = container.Name
        Me.ItemLimit = itemLimit
        _spContainer = container
        ChangeImageIcon()
    End Sub

    Public ReadOnly Property SiteUrl() As Uri
        Get
            If _siteUrl Is Nothing Then
                Select Case Container.ContainerType
                    Case SharePointContainer.TypeEnum.Site
                        _siteUrl = _spContainer.Path
                    Case Else
                        Dim parentSharePointNode As SharePointTreeNode
                        parentSharePointNode = CType(Parent, SharePointTreeNode)
                        _siteUrl = parentSharePointNode.SiteUrl
                End Select
            End If
            Return _siteUrl
        End Get
    End Property

    Public ReadOnly Property ID() As String
        Get
            Return _spContainer.Id()
        End Get
    End Property

    Public ReadOnly Property ContainerFolderPath() As String
        Get
            Select Case Container.ContainerType
                Case SharePointContainer.TypeEnum.Folder
                    If _containerFolderPath Is Nothing Then
                        '*** Get the container path less the SiteURL
                        Dim strPath As String = _spContainer.Path.ToString()
                        _containerFolderPath = strPath.Replace(SiteUrl.ToString(), "")
                    End If
            End Select
            Return _containerFolderPath
        End Get
    End Property

    Public ReadOnly Property Library() As SharePointLibrary
        Get
            Select Case Container.ContainerType
                Case SharePointContainer.TypeEnum.Library
                    Return CType(_spContainer, SharePointLibrary)
                Case SharePointContainer.TypeEnum.Folder
                    Dim parentSharePointNode As SharePointTreeNode
                    parentSharePointNode = CType(Parent, SharePointTreeNode)
                    If parentSharePointNode IsNot Nothing Then
                        Return parentSharePointNode.Library
                    Else
                        Return Nothing
                    End If
                Case Else
                    Return Nothing
            End Select
        End Get
    End Property

    Public ReadOnly Property MaxChildrenDisplayed() As Boolean
        Get
            Return Nodes.Count >= ItemLimit
        End Get
    End Property

    Public ReadOnly Property Container() As SharePointContainer
        Get
            Return _spContainer
        End Get
    End Property

    Public Property QueryState() As QueryStateEnum
        Get
            Return _queryState
        End Get
        Set(ByVal value As QueryStateEnum)
            _queryState = value
            ChangeImageIcon()
        End Set
    End Property

    Public Property ItemLimit As Integer

    Private Sub ChangeImageIcon()
        Dim bGray As Boolean = False
        
        If bGray Then
            ImageIndex = GetGrayImageIndex()
        Else
            ImageIndex = GetImageIndex()
        End If
        SelectedImageIndex = ImageIndex
    End Sub

    Private Function GetImageIndex() As Integer
        Select Case Container.ContainerType
            Case SharePointContainer.TypeEnum.Site
                Return 2
            Case SharePointContainer.TypeEnum.Library
                Return 4
            Case SharePointContainer.TypeEnum.Folder
                Return 6
            Case Else
                Throw New Exception()
        End Select
    End Function

    Private Function GetGrayImageIndex() As Integer
        Return GetImageIndex() + 1
    End Function

    Public Enum QueryStateEnum
        Unknown
        QueryCompleted
    End Enum

End Class

